* This script contains the code to replicate TableF of the on-line appendix in Ductor, L., Fafchamps, M., Goyal S. and M. van der Leij. Social Networks and Research Output. The Review of Economics and Statistics.
/*Using a 5-years window productivity variable*/
log using TableF.log, replace
use prodndnp_fullsample.dta
tsset auth year, yearly
gen lprodf5l=log(prod+L.prod+L2.prod+L3.prod+L4.prod+1)
gen lprodf5l2=L.lprodf5l
gen lprodf5l3=L2.lprodf5l
gen lprodf5l4=L3.lprodf5l
gen lprodf5l5=L4.lprodf5l
gen lprodf5l6=L5.lprodf5l
gen lprodf5l7=L6.lprodf5l
gen lprodf5l8=L7.lprodf5l
gen lprodf5l9=L8.lprodf5l
gen lprodf5l10=L9.lprodf5l
gen lprodf5l11=L10.lprodf5l
gen lprodf5l12=L11.lprodf5l
gen lprodf5l13=L12.lprodf5l
gen lprodf5l14=L13.lprodf5l
gen lprodf5l15=L14.lprodf5l
gen lprodf5l16=L15.lprodf5l

sort auth year
foreach i in 17 18 19 20 21 22 23 24 25 26 27{
gen lprodf5l`i'=L`i-1'.lprodf5l
replace lprodf5l`i'=0 if missing(lprodf5l`i')
}

/*Replacing missing by zeros*/
set more off
foreach i in lprodf5l lprodf5l2 lprodf5l3 lprodf5l4 lprodf5l5 lprodf5l6 lprodf5l7 lprodf5l8 lprodf5l9 lprodf5l10 lprodf5l11 lprodf5l12 lprodf5l13 lprodf5l14 lprodf5l15 lprodf5l16 lprodf5l17 lprodf5l18 lprodf5l19 lprodf5l20{
replace `i'=0 if missing(`i')
}

order auth year lcprod5 lcprodl5 lprodf5 lprodf5l-lprodf5l20 prod prodf5 qnt cprod cprod5 cprodl5 mnauthors ystart yend nopapers group prodf3 degree1y degree2y degree3y degree4y degree5y degree6y degree7y degree8y degree9y degree10y degree11y degree12y degree13y degree14y degree15y degree21y degree22y degree23y degree24y degree25y degree26y degree27y degree28y degree29y degree210y degree211y degree212y degree213y degree214y degree215y gc1y gc2y gc3y gc4y gc5y gc6y gc7y gc8y gc9y gc10y gc11y gc12y gc13y gc14y gc15y lnetprod1y lnetprod2y lnetprod3y lnetprod4y lnetprod5y lnetprod6y lnetprod7y lnetprod8y lnetprod9y lnetprod10y lnetprod11y lnetprod12y lnetprod13y lnetprod14y lnetprod15y lnetprod21y lnetprod22y lnetprod23y lnetprod24y lnetprod25y lnetprod26y lnetprod27y lnetprod28y lnetprod29y lnetprod210y lnetprod211y lnetprod212y lnetprod213y lnetprod214y lnetprod215y lbet1y lbet2y lbet3y lbet4y lbet5y lbet6y lbet7y lbet8y lbet9y lbet10y lbet11y lbet12y lbet13y lbet14y lbet15y


/*OLS out-of-sample program*/
reg lprodf5 y2-y25 nopapers t2-t25 if group==1, vce(cluster auth)
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf5<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrolsM0= (lprodf5-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrolsM0)if ybols<.  /*RSEE of the predicted model*/
quietly sum peols    /*summarizing the RSEE*/
quietly scalar rssols=r(max)  /*obtaining the total RSEE*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols

/* Model 1*/
/*OLS out-of-sample program*/

reg lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 if group==1, vce(cluster auth)
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf5<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrolsM1= (lprodf5-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrolsM1)if ybols<.  /*RSEE of the predicted model*/
quietly sum peols    /*summarizing the RSEE*/
quietly scalar rssols=r(max)  /*obtaining the total RSEE*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols


/*OLS out-of-sample program*/
cap pro drop ols_os
program define ols_os
syntax varlist(numeric min=1) [if]
marksample touse
reg `varlist' if `touse', vce(cluster auth)
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf5<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrols= (lprodf5-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrols)if ybols<.  /*RSEE of the predicted model*/
quietly sum peols    /*summarizing the RSEE*/
quietly scalar rssols=r(max)  /*obtaining the total RSEE*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols
end



/* Model 3*/

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 lnetprod1y lnetprod2y lnetprod3y lnetprod4y lnetprod5y lnetprod6y lnetprod7y if group==1 

/*Diebold-Mariano test*/
gen diffsqerrlM3=sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(11)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 lnetprod21y lnetprod22y lnetprod23y lnetprod24y lnetprod25y lnetprod26y lnetprod27y lnetprod28y lnetprod29y-lnetprod215y if group==1
gen diffsqerrlM3=sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 degree1y degree2y degree3y degree4y degree5y degree6y degree7y degree8y degree9y degree10y-degree15y if group==1
gen diffsqerrlM3= sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 degree21y degree22y degree23y degree24y degree25y degree26y degree27y degree28y degree29y degree210y degree211y degree212y degree213y degree214y degree215y if group==1
gen diffsqerrlM3= sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 gc1y gc2y-gc15y  if group==1
gen diffsqerrlM3=sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 gc1y gc2y-gc15y lbet1y-lbet15y  if group==1
gen diffsqerrlM3= sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3
 
ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 gc1y gc2y-gc15y lclos1y-lclos15y  if group==1
gen diffsqerrlM3=sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM3

ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 neiq1fs1y neiq1fs2y neiq1fs3y neiq1fs4y neiq1fs5y neiq1fs6y neiq1fs7y neiq1fs8y neiq1fs9y if group==1
gen diffsqerrlM3=sqerrols-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf5<., lag(11)
drop sqerrols diffsqerrlM3

/*Model 2*/
ols_os lprodf5 y2-y25 nopapers t2-t25  lnetprod1y lnetprod2y lnetprod3y lnetprod4y lnetprod5y lnetprod6y lnetprod7y lnetprod8y-lnetprod13y if group==1 
gen diffsqerrlM2= sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(13)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  lnetprod21y lnetprod22y lnetprod23y lnetprod24y lnetprod25y lnetprod26y lnetprod27y lnetprod28y lnetprod29y lnetprod210y lnetprod211y lnetprod212y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(12)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  degree1y degree2y degree3y degree4y degree5y degree6y degree7y degree8y degree9y degree10y degree11y degree12y degree13y  if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(13)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  degree21y degree22y degree23y degree24y degree25y degree26y degree27y degree28y degree29y degree210y degree211y degree212y degree213y  if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(13)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  gc1y gc2y-gc15y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  gc1y gc2y-gc12y lbet1y-lbet12y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(12)
drop sqerrols diffsqerrlM2


ols_os lprodf5 y2-y25 nopapers t2-t25  gc1y gc2y-gc15y lclos1y-lclos15y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(15)
drop sqerrols diffsqerrlM2

ols_os lprodf5 y2-y25 nopapers t2-t25  neiq1fs1y neiq1fs2y neiq1fs3y neiq1fs4y neiq1fs5y neiq1fs6y neiq1fs7y neiq1fs8y neiq1fs9y neiq1fs10y neiq1fs11y neiq1fs12y neiq1fs13y neiq1fs14y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(14)
drop sqerrols diffsqerrlM2

/*Mv Model 2*/
ols_os lprodf5 y2-y25 nopapers t2-t25 lnetprod1y-lnetprod13y lnetprod21y-lnetprod213y degree1y-degree13y degree21y-degree213y gc1y-gc13y lbet1y-lbet13y lclos1y-lclos13y neiq1fs1y-neiq1fs13y if group==1
gen diffsqerrlM2=sqerrols-sqerrolsM1
newey2 diffsqerrlM2 if group==2 & lprodf5<., lag(13)
drop sqerrols diffsqerrlM2

/*Mv Model 3*/
ols_os lprodf5 y2-y25 nopapers t2-t25 lprodf5l-lprodf5l11 lnetprod1y-lnetprod9y lnetprod21y-lnetprod29y degree1y-degree9y degree21y-degree29y gc1y-gc9y lbet1y-lbet9y lclos1y-lclos9y neiq1fs1y-neiq1fs9y if group==1
gen diffsqerrlMV3=sqerrols-sqerrolsM1
newey2 diffsqerrlMV3 if group==2 & lprodf5<., lag(11)
drop sqerrols diffsqerrlMV3

log close


